from Prinz_classes import *
from pylab import *
from genutils.io import *
c = Ccell('D_cell_config')
c.set_synaptic_time(0, 0)
dt = 0.1
sim_length = 5000 # length of simulation in msec
c.exercise(timestep=dt, i_time=sim_length)
v_trace = c.V_trace()
from cell_test_functions import *
spikes = spike_detect(v_trace, 0)
bursts = burst_detect(spikes, 5) # find the bursts
P_0 = 0
time_0 = 0
if len(bursts) > 2:
    P_0 = (bursts[2][0] - bursts[1][0])*dt # this is the original burst period
    time_0 = (bursts[1][0])*dt # the start time of the first burst
    print time_0
    time_1 = (bursts[2][0])*dt # the start time of the second burst
    print time_1
onsets = linspace(0+P_0*0.01, P_0-P_0*0.01, 10) # the times of the onsets relative to the fist burst
period_changes = []
for prc_i in onsets:
    print prc_i # this is the onset time
    # this sets the onset to be the time of the first spike of the burst + the onset
    # the duration is 25% of the burst period
    c.set_synaptic_time(time_0+prc_i, P_0/4) 
    c.exercise(timestep=dt, i_time=sim_length) # run the simulation
    v_trace = c.V_trace()
    spikes = spike_detect(v_trace, 0)
    bursts = burst_detect(spikes, 5) # find the bursts
    if len(bursts) > 2:
        # calculate the difference between the current burst period and the old burst period
        delta_P = ((float(bursts[2][0]) - float(bursts[1][0]))*dt)-P_0
        # add the ratio of the period change and the period
        period_changes.append((delta_P)/float(P_0))
onsets = onsets/P_0 # changes from onsets to phase
plot(period_changes)
show()    
